package com.saint.easemobtest;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class MainActivity extends AppCompatActivity {

    private EditText userId_et;
    private Button getChat_btn,logout_btn;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1://发起会话


                    break;
                case 2://退出成功
                    Toast.makeText(MainActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case 3://退出失败
                    int code = msg.arg1;
                    String error = (String) msg.obj;
                    Toast.makeText(MainActivity.this, "退出失败： code: " + code + "  error: " + error , Toast.LENGTH_LONG).show();
                    break;

                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userId_et = (EditText) findViewById(R.id.user_name_et);
        getChat_btn = (Button) findViewById(R.id.get_chat_btn);
        logout_btn = (Button) findViewById(R.id.logout_btn);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String packageName = getPackageName();
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                try {
                    //some device doesn't has activity to handle this intent
                    //so add try catch
                    Intent intent = new Intent();
                    intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                    intent.setData(Uri.parse("package:" + packageName));
                    startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(this, "电池优化忽略设置未成功，请到手机电池设置添加", Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.get_chat_btn:
                getChat();
                break;
            case R.id.logout_btn:
                logout();
                break;

            default:
                break;
        }
    }

    private void logout(){
        EMClient.getInstance().logout(true, new EMCallBack() {
            @Override
            public void onSuccess() {
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onError(int code, String error) {
                Message msg = handler.obtainMessage();
                msg.what = 3;
                msg.arg1 = code;
                msg.obj = error;
                handler.sendMessage(msg);
            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });
    }

    private void getChat(){
        // 获取我们发起聊天的者的username
        String chatId = userId_et.getText().toString().trim();
        if (!TextUtils.isEmpty(chatId)) {
            // 获取当前登录用户的 username
            String currUsername = EMClient.getInstance().getCurrentUser();
            if (chatId.equals(currUsername)) {
                Toast.makeText(MainActivity.this, "不能和自己聊天", Toast.LENGTH_SHORT).show();
                return;
            }
            Log.e("MainActivity", "执行跳转______________");
            // 跳转到聊天界面，开始聊天
            Intent intent = new Intent(MainActivity.this, TestChatActivity.class);
            intent.putExtra("ec_chat_id", chatId);
            startActivity(intent);
            Log.e("MainActivity", "执行跳转成功______________");
        } else {
            Toast.makeText(MainActivity.this, "Username 不能为空", Toast.LENGTH_LONG).show();
        }
    }
}
